package com.ch.forum.dao;

import com.ch.forum.bo.ReplyListVo;
import com.ch.forum.model.ReplyModel;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ReplyMapper {

    @Insert("insert into t_reply(art_id,content,date,is_pre,re_user_id,user_id,zan_num,re_reply_num) " +
            "values(#{vo.artId},#{vo.content},#{vo.date},#{vo.isPre},#{vo.reUserId},#{vo.userId},#{vo.zanNum},#{vo.reReplyNum})")
    @Options(useGeneratedKeys = true, keyProperty = "vo.id")
    Long addReply(@Param("vo") ReplyModel vo);

    @Select("select re_reply_num from t_reply where id = #{id}")
    int findReplyNumById(@Param("id") Long id);

    @Select("select id,date,art_id as artId,content,re_user_id as reUserId,user_id as userId,zan_num as zanNum,re_reply_num as reReplyNum,(SELECT u.nick_name FROM t_user u WHERE u.id = user_id) as nickName from t_reply " +
            "where is_pre = 0 and art_id = #{artId} and re_user_id is null order by date limit #{pageNo},#{pageSize}")
    List<ReplyListVo> findReplyPageByArtId(@Param("artId")Long artId, @Param("pageNo")int pageNo, @Param("pageSize")int pageSize);

    @Select("select id,date,art_id as artId,content,re_user_id as reUserId,user_id as userId,zan_num as zanNum,re_reply_num as reReplyNum from t_reply " +
            "where is_pre = 0 and user_id = #{userId} and re_user_id is null order by date limit #{pageNo},#{pageSize}")
    List<ReplyListVo> findMyReplyByUserId(@Param("userId")Long userId, @Param("pageNo")int pageNo, @Param("pageSize")int pageSize);

    @Select("select count(1) from t_reply where is_pre = 0 and user_id = #{userId} and re_user_id is null")
    int countMyReplyByUserId(@Param("userId")Long userId);

    @Select("select count(1) from t_reply where is_pre = 0 and art_id = #{artId} and re_user_id is null")
    int countReplyByArtId(@Param("artId")Long artId);

    @Select("select id from t_reply where is_pre = 0 order by date desc")
    List<Long> findPreReplyIds();

    @Update("update t_reply set re_reply_num = #{num}")
    int updateReplyNum(@Param("num") int num);

    @Update("update t_reply set is_pre = 0 where id in (#{ids})")
    int updateIsPre(@Param("ids")String ids);
}
